package vn.ducquoc.cassandra;

import java.nio.ByteBuffer;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.TBinaryProtocol;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

/**
 * Cassandra Delete example.
 * 
 * @author ducquoc
 */
public class CassandraThriftDelete {
  public static final String CASSANDRA_HOST = "192.168.22.84";// "tlcirrusa03";
  public static final int CASSANDRA_PORT = 9160;

  public static final String SAMPLE_KEYSPACE = "Keyspace1";
  public static final String SAMPLE_COLUMN_FAMILY = "Standard1";

  public static final ByteBuffer COLUMN = ByteBufferUtil.bytes("2012-01-01_12:12:12");
  public static final ByteBuffer VALUE = ByteBufferUtil.bytes("{\"test\":\"42\"}");

  static final long TIMESTAMP = 1010102011000L;//System.currentTimeMillis();

  public static void main(String[] args) throws Exception {

    TTransport transport = new TFramedTransport(new TSocket(CASSANDRA_HOST,
            CASSANDRA_PORT));
    TProtocol protocol = new TBinaryProtocol(transport);
    Cassandra.Client client = new Cassandra.Client(protocol);
    transport.open();

    client.set_keyspace(SAMPLE_KEYSPACE);

    // specify column path
    ColumnPath path = new ColumnPath();
    path.setColumn_family(SAMPLE_COLUMN_FAMILY);
    path.setColumn(ByteBuffer.wrap("2012-01-01_12:12:12".getBytes()));

    // define row id
    ByteBuffer rowkeyId = ByteBuffer.wrap("PO123".getBytes("UTF-8"));

    // specify timestamp value of a rowkey - see CassandraThriftInsert
    long timestamp = TIMESTAMP;

    // define consistency level
    ConsistencyLevel consistencyLevel = ConsistencyLevel.ANY;

    // execute delete
    client.remove(rowkeyId, path, timestamp, consistencyLevel);
    System.out.println("Deletion/Removal Thrift " + client.describe_version());

    // release resources
    transport.flush();
    transport.close();
  }

}